package com.frame.pageform.component.validata;

import java.awt.image.BufferedImage;

import javax.servlet.http.HttpSession;

public class ValidateNumber {

	static protected final String NAME = "$V$validate-number$";
	private static String number;
	private static ValidateNumberGenerator generator=new ValidateNumberGenerator();

	/**
	 * 区分大小写验证
	 * @param number
	 * @param session
	 * @return
	 */
	static public boolean check(String number, HttpSession session)
	{
		String theNumber = (String) session.getAttribute(NAME);

		if (theNumber == null)
			return false;
		return theNumber.equals(number);
	}
	/**
	 * 不区分大小写验证
	 * @param number
	 * @param session
	 * @return
	 */
	static public boolean checkAll(String number, HttpSession session)
	{
		String theNumber = (String) session.getAttribute(NAME);
		if (theNumber == null)
			return false;
		theNumber=theNumber.toLowerCase();
		number=number.toLowerCase();
		return theNumber.equals(number);
	}

	static public void clearNumber(HttpSession session){
		session.removeAttribute(NAME);
	}
	public static BufferedImage createImgNumber(HttpSession session){
		number=generator.generateNumber();
		session.setAttribute(NAME, number);
		return generator.generateImage(number);
	}

	public static void setGenerator(ValidateNumberGenerator generator) {
		ValidateNumber.generator = generator;
	}
	public String getNumber() {
		return number;
	}
}
